﻿using System;
using MySql.Data.Types;

namespace ResearchRepository.Models
{
	public class RRRequest
	{
		//TODO create some case-specific constructors
		public RRRequest(int rID = 0, /*int rTypeID,*/ RRRequestType rType = RRRequestType.General, int usID = RRUser.GuestUserID, string usName = null, int prID = 0, string prName = null, string n = "",
							MySqlDateTime dSubmitted = new MySqlDateTime(), /* int sID = 0, */ RRStatusType sType = RRStatusType.Pending,
							MySqlDateTime dAdmined = new MySqlDateTime(), int aID = 0,
							string newEmail = null, string newPre = null, string newFirst = null,
							string newMiddle = null, string newLast = null, string newPost = null,
							int unID = 0, string newUniv = null, int deID = 0, string newDept = null,
							string newProj = null, int diID = 0, int peID = 0, RRPermissionType peType = RRPermissionType.None, int fID = 0)
		{
			ReqID = rID;
			/* ReqTypeID = rTypeID;*/
			ReqType = rType;
			UserID = usID;
			UserName = usName;
			ProjID = prID;
			ProjName = prName;
			Note = n;
			DateSubmitted = dSubmitted;
			/* StatID = sID; */
			StatType = sType;
			DateAdmined = dAdmined;
			AdminID = aID;
			NewUserEmail = newEmail;
			NewUserPreTitles = newPre;
			NewUserFirstName = newFirst;
			NewUserMiddleName = newMiddle;
			NewUserLastName = newLast;
			NewUserPostTitles = newPost;
			UnivID = unID;
			NewUnivName = newUniv;
			DeptID = deID;
			NewDeptName = newDept;
			NewProjName = newProj;
			DiscID = diID;
			PermID = peID;
			PermType = peType;
			FileID = fID;
		}
		public RRRequest(RRRequest source)
			:this(source.ReqID,
					/* source.ReqTypeID, */
					source.ReqType,
					source.UserID,
					source.UserName,
					source.ProjID,
					source.ProjName,
					source.Note,
					source.DateSubmitted,
					/* source.StatID, */
					source.StatType,
					source.DateAdmined,
					source.AdminID,
					source.NewUserEmail,
					source.NewUserPreTitles,
					source.NewUserFirstName,
					source.NewUserMiddleName,
					source.NewUserLastName,
					source.NewUserPostTitles,
					source.UnivID,
					source.NewUnivName,
					source.DeptID,
					source.NewDeptName,
					source.NewProjName,
					source.DiscID,
					source.PermID,
					source.PermType,
					source.FileID)
		{}
		public RRRequest(System.Data.IDataRecord source)
			:this((int) source["reqID"],
					/* (int) source["ReqTypeID"], */
					(RRRequestType) Enum.Parse(typeof(RRRequestType), (string) source["reqTypeName"], true),
					(int) source["userID"],
					source["userName"] != DBNull.Value ? (string) source["userName"] : null,	// userName may be NULL
					source["projID"] != DBNull.Value ? (int) source["projID"] : 0,				// projID may be NULL
					source["projName"] != DBNull.Value ? (string) source["projName"] : null,	// projName may be NULL
					(string) source["note"],
					(MySqlDateTime) source["dateSubmitted"],
					/* (int) source["statID"], */
					(RRStatusType) Enum.Parse(typeof(RRStatusType), (string) source["statName"], true),
				/* all of the following properties may be NULL */
					source["dateAdmined"] != DBNull.Value ? (MySqlDateTime) source["dateAdmined"] : new MySqlDateTime(0,0,0,0,0,0),
					source["adminID"] != DBNull.Value ? (int) source["adminID"] : 0,
					source["newUserEmail"] != DBNull.Value ? (string) source["newUserEmail"] : null,
					source["newUserPreTitles"] != DBNull.Value ? (string) source["newUserPreTitles"] : null,
					source["newUserFirstName"] != DBNull.Value ? (string) source["newUserFirstName"] : null,
					source["newUserMiddleName"] != DBNull.Value ? (string) source["newUserMiddleName"] : null,
					source["newUserLastName"] != DBNull.Value ? (string) source["newUserLastName"] : null,
					source["newUserPostTitles"] != DBNull.Value ? (string) source["newUserPostTitles"] : null,
					source["univID"] != DBNull.Value ? (int) source["univID"] : 0,
					source["newUnivName"] != DBNull.Value ? (string) source["newUnivName"] : null,
					source["deptID"] != DBNull.Value ? (int) source["deptID"] : 0,
					source["newDeptName"] != DBNull.Value ? (string) source["newDeptName"] : null,
					source["newProjName"] != DBNull.Value ? (string) source["newProjName"] : null,
					source["discID"] != DBNull.Value ? (int) source["discID"] : 0,
					source["permID"] != DBNull.Value ? (int) source["permID"] : 0,
					source["permName"] != DBNull.Value ? (RRPermissionType) Enum.Parse(typeof(RRPermissionType), (string) source["permName"], true) : RRPermissionType.None,
					source["fileID"] != DBNull.Value ? (int) source["fileID"] : 0)
		{}

		// named alias for constructor, provides handle to simulate a function pointer to constructor
		public static RRRequest construct(System.Data.IDataRecord source)
		{
			return new RRRequest(source);
		}

		/* common fields */
		public int ReqID { get; set; }
		/* public int ReqTypeID { get; set; } */
		public RRRequestType ReqType { get; set; }
		public int UserID { get; set; }
		public string UserName { get; set; }
		public int ProjID { get; set; }
		public string ProjName { get; set; }
		public string Note { get; set; }				/* 64Kb of note(s) from/to/between user and admin */
		public MySqlDateTime DateSubmitted { get; set; }
		/* public int StatID { get; set; } */
		public RRStatusType StatType { get; set; }
		public MySqlDateTime DateAdmined { get; set; }
		public int AdminID { get; set; }
		
		/* fields used for different request types, no one type is expected to used every field */
		public string NewUserEmail { get; set; }		/* new user requests */
		public string NewUserPreTitles { get; set; }	/* new user requests */
		public string NewUserFirstName { get; set; }	/* new user requests */
		public string NewUserMiddleName { get; set; }	/* new user requests */
		public string NewUserLastName { get; set; }		/* new user requests */
		public string NewUserPostTitles { get; set; }	/* new user requests */
		public int UnivID { get; set; }					/* new user requests */
		public string NewUnivName { get; set; }			/* new user requests */
		public int DeptID { get; set; }					/* new user requests */
		public string NewDeptName { get; set; }			/* new user requests */

		public string NewProjName { get; set; }			/* new project requests */
		public int DiscID { get; set; }					/* new project requests */
		
		public int PermID { get; set; }					/* new permissions requests */
		public RRPermissionType PermType { get; set; }
		public int FileID { get; set; }					/* file and new permissions requests */

		/* TODO add related fields returned as part of DB fetch */
	}
}